import java.util.Scanner;
import java.util.Stack;

public class DecimalToOtherBase {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("输入一个十进制数字:");
        int decimalNumber = scanner.nextInt();

        System.out.println("输入想要转换为几进制的数字(2, 8, 16):");
        int targetBase = scanner.nextInt();

        if (targetBase == 2 || targetBase == 8 || targetBase == 16) {
            String result = convertToBase(decimalNumber, targetBase);
            System.out.println("转换后的结果为: " + result);
        } else {
            System.out.println("输入的进制不正确，请输入2、8或16。");
        }

        scanner.close();
    }

    public static String convertToBase(int decimalNumber, int targetBase) {
        Stack<Integer> stack = new Stack<>();
        String digits = "0123456789ABCDEF";

        while (decimalNumber > 0) {
            int remainder = decimalNumber % targetBase;
            stack.push(remainder);
            decimalNumber /= targetBase;
        }

        StringBuilder result = new StringBuilder();
        while (!stack.isEmpty()) {
            result.append(digits.charAt(stack.pop()));
        }

        return result.toString();
    }
}